package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.mygdx.game.actor.DemoActor;

/**
 * 弹窗示例
 * Created by 80002023 on 2016/8/5.
 */
public class WindowDemo extends Game {

    SpriteBatch batch;
    Stage stage;

    Window exitWindow;

    BitmapFont font;

    @Override
    public void create() {
        batch = new SpriteBatch();
        stage = new Stage();

        Gdx.input.setInputProcessor(stage);

        font = new BitmapFont(Gdx.files.internal("data/font/potato.fnt"), Gdx.files.internal("data/font/potato.png"), false);

        Texture texture = new Texture(Gdx.files.internal("data/btn/control2.png"));

        TextureRegion[][] split = TextureRegion.split(texture, 64, 64);

        TextureRegion[] regions = new TextureRegion[6];

        regions[0] = split[0][0];
        regions[1] = split[0][1];
        regions[2] = split[0][2];
        regions[3] = split[0][3];
        regions[4] = split[1][0];
        regions[5] = split[1][1];

        ImageButton showWin = new ImageButton(new TextureRegionDrawable(regions[0]), new TextureRegionDrawable(regions[1]));

        showWin.addListener(new ClickListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                stage.addActor(exitWindow);
                return false;
            }
        });

        stage.addActor(showWin);

        createWindow();

        ImageButton ok = new ImageButton(new TextureRegionDrawable(regions[2]), new TextureRegionDrawable(regions[3]));

        ok.setPosition(exitWindow.getWidth() / 4, 0);
        ok.addListener(new ClickListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.exit();
                return false;
            }
        });

        ImageButton cancel = new ImageButton(new TextureRegionDrawable(regions[4]), new TextureRegionDrawable(regions[5]));
        cancel.setPosition(exitWindow.getWidth() / 2, 0);
        cancel.addListener(new ClickListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                exitWindow.remove();
                return false;
            }
        });

        exitWindow.addActor(ok);
        exitWindow.addActor(cancel);

        DemoActor demoA = new DemoActor();
        demoA.setPosition(50,50);
        stage.addActor(demoA);
    }

    private void createWindow() {
        TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/dialog/dialog.png"))));

        Window.WindowStyle style = new Window.WindowStyle(font, Color.RED, drawable);
        exitWindow = new Window("Exit", style);
        exitWindow.setWidth(Gdx.graphics.getWidth() / 1.5f);
        exitWindow.setHeight(Gdx.graphics.getHeight() / 1.5f);
        exitWindow.setPosition(100, 80);
        exitWindow.setMovable(true);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        font.draw(batch, "Press Button to exit app!", 100, 200);
        batch.end();
        stage.act();
        stage.draw();
    }
}
